﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

public class Z1029WindowControl : MonoBehaviour
{
    public Text windowName;

    void Start()
    {
        windowName.text = gameObject.name;
    }

    void Update()
    {
        
    }

    public void StartMoveWindow()
    {
        StartCoroutine(MoveWindow());
        SetWindowToFront();
    }

    public void SetWindowToFront()
    {
        transform.SetAsLastSibling();
    }

    public void StartResizeWindow(int direction)//0:Left ,1:Right ,2:Top ,3:Bottom
    {
        if (direction == 0) StartCoroutine(ResizeWindowX(true));
        if (direction == 1) StartCoroutine(ResizeWindowX(false));
        if (direction == 2) StartCoroutine(ResizeWindowY(true));
        if (direction == 3) StartCoroutine(ResizeWindowY(false));
    }

    public IEnumerator ResizeWindowX(bool dir)
    {
        for (; ; )
        {
            if (Input.GetMouseButtonUp(0))
                break;
            ((RectTransform)transform).anchoredPosition += new Vector2(MousePosition.mouseMove.x/2, 0);

            if(dir)((RectTransform)transform).sizeDelta += new Vector2(MousePosition.mouseMove.x, 0);
            else((RectTransform)transform).sizeDelta -= new Vector2(MousePosition.mouseMove.x, 0);
            yield return null;
        }
    }

    public IEnumerator ResizeWindowY(bool dir)
    {
        for (; ; )
        {
            if (Input.GetMouseButtonUp(0))
                break;
            ((RectTransform)transform).anchoredPosition += new Vector2(0, MousePosition.mouseMove.y/2);

            if(dir)((RectTransform)transform).sizeDelta += new Vector2(0,MousePosition.mouseMove.y);
            else ((RectTransform)transform).sizeDelta -= new Vector2(0,MousePosition.mouseMove.y);
            yield return null;
        }
    }

    public IEnumerator MoveWindow()
    {
        for(; ; )
        {
            if (Input.GetMouseButtonUp(0))
                break;
            ((RectTransform)transform).anchoredPosition += MousePosition.mouseMove;
            yield return null;
        }
    }
}